home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / ipc / mainmsgqcli1.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  65 lines

  1. #include    <stdio.h>
  2. #include    "mesg.h"
  3. #include    "msgq.h"
  4.  
  5. Mesg    mesg;
  6.  
  7. main()
  8. {
  9.     int    id;
  10.  
  11.     /*
  12.      * Open the single message queue.  The server must have
  13.      * already created it.
  14.      */
  15.  
  16.     if ( (id = msgget(MKEY1, 0)) < 0)
  17.         err_sys("client: can't msgget message queue 1");
  18.  
  19.     client(id);
  20.  
  21.     /*
  22.      * Now we can delete the message queue.
  23.      */
  24.  
  25.     if (msgctl(id, IPC_RMID, (struct msqid_ds *) 0) < 0)
  26.         err_sys("client: can't RMID message queue 1");
  27.  
  28.     exit(0);
  29. }
  30.  
  31. client(id)
  32. int    id;
  33. {
  34.     int    n;
  35.  
  36.     /*
  37.      * Read the filename from standard input, write it as
  38.      * a message to the IPC descriptor.
  39.      */
  40.  
  41.     if (fgets(mesg.mesg_data, MAXMESGDATA, stdin) == NULL)
  42.         err_sys("filename read error");
  43.  
  44.     n = strlen(mesg.mesg_data);
  45.     if (mesg.mesg_data[n-1] == '\n')
  46.         n--;            /* ignore the newline from fgets() */
  47.     mesg.mesg_data[n] = '\0';    /* overwrite newline at end */
  48.     mesg.mesg_len = n;
  49.     mesg.mesg_type = 1L;        /* send messages of this type */
  50.     mesg_send(id, &mesg);
  51.  
  52.     /*
  53.      * Receive the message from the IPC descriptor and write
  54.      * the data to the standard output.
  55.      */
  56.  
  57.     mesg.mesg_type = 2L;    /* receive messages of this type */
  58.     while( (n = mesg_recv(id, &mesg)) > 0)
  59.         if (write(1, mesg.mesg_data, n) != n)
  60.             err_sys("data write error");
  61.  
  62.     if (n < 0)
  63.         err_sys("data read error");
  64. }
  65.